package com.hst.web.common.utils;

import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * @author yangshankun
 * @desc scp文件拷贝工具类
 * @date 2024/5/3 18:52
 */

@Slf4j
public class ScpUtil {

    private static JSch instance = null;

    private static Session session = null;

    public static JSch getInstance() {
        synchronized (ScpUtil.class) {
            if (instance != null) {
                return instance;
            } else {
                instance = new JSch();
                return instance;
            }
        }
    }


    /**
     * 创建会话
     *
     * @param jSch
     * @param hostIp
     * @param username
     * @param pass
     * @return
     */
    public static void initSession(JSch jSch, String username, String pass, String hostIp) {
        try {
            ScpUtil.session = jSch.getSession(username, hostIp, 22);
            session.setPassword(pass);
            // 设置第一次登陆时提示，可选值：(ask | yes | no)
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
        } catch (JSchException e) {
            log.error("创建scp 会话异常", e);
        }
    }

    public static void uploadFiles(List<File> srcFiles, String destPath) {
        if (session == null) {
            log.error("session 未初始化!");
            return;
        }
        ChannelSftp sftpClient = null;
        FileInputStream fis = null;
        try {
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftpClient = (ChannelSftp) channel;
            // sftpClient.mkdir(newRemoteDirPath);
            for (File srcFile : srcFiles) {
                // 文件为目录，跳过
                if (srcFile.isDirectory()) {
                    continue;
                }
                fis = new FileInputStream(srcFile);
                // 创建文件
                String destFile = destPath + srcFile.getName();
//                String command = "touch " + destFile; // 创建文件的命令，这里使用touch命令创建空文件
//                Channel execChannel = session.openChannel("exec");
//                ((ChannelExec) execChannel).setCommand(command);
//                channel.setInputStream(null);
//                ((ChannelExec) execChannel).setErrStream(System.err);
//                execChannel.connect();
//                execChannel.disconnect();
                sftpClient.put(fis, destFile);
                fis.close();
            }
            log.info("=========================文件上传成功！");
        } catch (JSchException | SftpException e) {
            log.error("创建通道异常", e);
        } catch (FileNotFoundException e) {
            log.error("打开文件错误，文件不存在", e);
        } catch (Exception e) {
            log.error("上传文件异常", e);
        } finally {
            if (sftpClient != null) {
                sftpClient.quit();
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            session.disconnect();
        }
    }
}
